package com.ymatou.api.sellerpromotion.testcase;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.sun.org.apache.bcel.internal.generic.NEW;
import com.ymatou.api.marketingseller.service.CreateSellerPromotionCall;
import com.ymatou.api.sellerpromotion.service.GetProductPromotionPolicyCall;
import com.ymatou.api.sellerpromotion.service.GetPromotionProductByIdCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.SellerPromotionCallService;
import com.ymttest.database.model.YmtSellerPromotionProducts;
import com.ymttest.database.mongo.SellerPromotionInfoWapper;
import com.ymttest.database.sqlwapper.YmtSellerPromotionProductsIMapper;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;



public class Ts_GetProductPromotionPolicy {
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取买手商品促销策略信息");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new GetProductPromotionPolicyCall().getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductPromotionPolicy_001() {
		Logger.start(true, "指定商品A的满减促销，使用商品A查询,查询结果正确");
		try {
			int sellerId = 20323743;
			SellerPromotionCallService.checkIsExistPromotion(sellerId);
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("PromotionInfo->PromotionName","满2件减8.8-自动");
			map.put("PromotionInfo->PromotionBeginTime",YMTDateUtil.getBeforeOrNextDay(0));
			map.put("PromotionInfo->PromotionEndTime",YMTDateUtil.getBeforeOrNextDay(3));
			map.put("PromotionInfo->PromotionType","1");
			map.put("PromotionInfo->IsSpecifyProduct",true);
			String productId = "74b5c894-8ee7-47d2-a787-911d5602bb68";
			map.put("PromotionInfo->ProductIds",new JSONArray("[74b5c894-8ee7-47d2-a787-911d5602bb68]"));
			JSONObject retBea = new CreateSellerPromotionCall().callAndGetReturnData(map);
			
			int promotionId = retBea.getJSONObject("Data").getInt("PromotionId");
			YMTDateUtil.waitTime(20);
			Map<String, Object> mongoDataMap = new SellerPromotionInfoWapper().getPromotionId(promotionId);
			
			JSONObject GetProductPromotionPolicyBean = new GetPromotionProductByIdCall().callAndGetReturnData(MapUtil.hashMap("sellerId", sellerId,"ProductId", productId));
			Logger.verifyEquals(200,GetProductPromotionPolicyBean.getInt("Code"), "响应报文-返回码");			
			/// 校验mongo 数据库数据
			JSONObject PromotionInfoBean = GetProductPromotionPolicyBean.getJSONObject("Data").getJSONObject("PromotionPolicys");
			
			Logger.verifyEquals(PromotionInfoBean.getInt("SellerId"), mongoDataMap.get("SellerId"), "买手ID");
			Logger.verifyEquals(PromotionInfoBean.getString("PromotionName"), mongoDataMap.get("PromotionName"), "满减促销名称");
			Logger.verifyEquals(PromotionInfoBean.getInt("PromotionType"), mongoDataMap.get("PromotionType"), "满减促销类型");
			Logger.verifyEquals(PromotionInfoBean.getString("PromotionDesc"), mongoDataMap.get("PromotionDesc"), "满减促销描述");
			AssertService.assertTime(YMTDateUtil.parseYMDHMSDate(YMTDateUtil.parseRespDate2(PromotionInfoBean.getString("PromotionBeginTime"))), (Date)mongoDataMap.get("PromotionBeginTime"), 60);
			AssertService.assertTime(YMTDateUtil.parseYMDHMSDate(YMTDateUtil.parseRespDate2(PromotionInfoBean.getString("PromotionEndTime"))), (Date)mongoDataMap.get("PromotionEndTime"), 60);
			Logger.verifyEquals(PromotionInfoBean.getInt("PromotionLimitNum"), mongoDataMap.get("PromotionLimitNum"), "满减促销买家限购数");
			Logger.verifyEquals(PromotionInfoBean.getBoolean("CanUseSellerCoupon"), mongoDataMap.get("CanUseSellerCoupon"), "满减促销是否使用买手优惠券");
			Logger.verifyEquals(PromotionInfoBean.getBoolean("IsSameProductUse"), mongoDataMap.get("IsSameProductUse"), "是否同商品满减");
			Logger.verifyEquals(PromotionInfoBean.getBoolean("IsSpecifyProduct"), mongoDataMap.get("IsSpecifyProduct"), "是否指定商品活动");
			Logger.verifyEquals(PromotionInfoBean.getBoolean("IsLimitProduct"), mongoDataMap.get("IsLimitProduct"), "是否为排除商品活动");	
			Logger.verifyEquals(PromotionInfoBean.getJSONArray("PromotionValues").getJSONObject(0).getInt("QtyCondition"), new JSONArray(mongoDataMap.get("PromotionValues").toString()).getJSONObject(0).getInt("QtyCondition"), "满减促销件数");
			Logger.verifyEquals(Float.valueOf(PromotionInfoBean.getJSONArray("PromotionValues").getJSONObject(0).getString("Value")), Float.valueOf(new JSONArray(mongoDataMap.get("PromotionValues").toString()).getJSONObject(0).getString("Value")), "优惠折扣");
			Logger.verifyEquals(Float.valueOf(PromotionInfoBean.getJSONArray("PromotionValues").getJSONObject(0).getInt("AmountCondition")), Float.valueOf(new JSONArray(mongoDataMap.get("PromotionValues").toString()).getJSONObject(0).getString("AmountCondition")), "优惠折扣");
			
			JSONArray prodSqlArray = new JSONArray();
			for(int i = 0;i<PromotionInfoBean.getJSONArray("PromotionProducts").length();i++){
				prodSqlArray.put(PromotionInfoBean.getJSONArray("PromotionProducts").getJSONObject(i).get("ProductId"));
			}
			
			JSONArray productIds = new JSONArray(mongoDataMap.get("ProductIds").toString());
			AssertService.checkArray(productIds,prodSqlArray);
		} catch (Exception e) {
		
	}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductPromotionPolicy_002() {
		Logger.start(true, "排除商品A的满减促销，使用商品A查询,结果为空");
		try {
			int sellerId = 20323743;
			SellerPromotionCallService.checkIsExistPromotion(sellerId);
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("PromotionInfo->PromotionName","满2件减8.8-自动");
			map.put("PromotionInfo->PromotionBeginTime",YMTDateUtil.getBeforeOrNextDay(0));
			map.put("PromotionInfo->PromotionEndTime",YMTDateUtil.getBeforeOrNextDay(3));
			map.put("PromotionInfo->PromotionType","1");
			map.put("PromotionInfo->IsLimitProduct",true);
			String productId = "74b5c894-8ee7-47d2-a787-911d5602bb68";
			map.put("PromotionInfo->ProductIds",new JSONArray("[74b5c894-8ee7-47d2-a787-911d5602bb68]"));
			JSONObject retBea = new CreateSellerPromotionCall().callAndGetReturnData(map);
			
			int promotionId = retBea.getJSONObject("Data").getInt("PromotionId");
			Map<String, Object> mongoDataMap = new SellerPromotionInfoWapper().getPromotionId(promotionId);
			JSONObject GetProductPromotionPolicyBean = new GetPromotionProductByIdCall().callAndGetReturnData(MapUtil.hashMap("sellerId", sellerId,"ProductId", productId));
			
			JSONArray productIds = new JSONArray(mongoDataMap.get("ProductIds").toString());
			AssertService.checkArray(productIds,new JSONArray("[74b5c894-8ee7-47d2-a787-911d5602bb68]"));
			Logger.verifyEquals(true, mongoDataMap.get("IsLimitProduct"), "是否为排除商品活动");
			Logger.verifyEquals(200,GetProductPromotionPolicyBean.getInt("Code"), "响应报文-返回码");			
			Logger.verifyIsNull(GetProductPromotionPolicyBean.getString("Data"), "查询结果为空");
			
		} catch (Exception e) {
		
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductPromotionPolicy_003() {
		Logger.start(true, "商品在未开始的活动中，查询结果为空");
		try {
			int sellerId = 20323743;
			SellerPromotionCallService.checkIsExistPromotion(sellerId);
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("PromotionInfo->PromotionName","满2件减8.8-自动");
			map.put("PromotionInfo->PromotionBeginTime",YMTDateUtil.getBeforeOrNextDay(-1));
			map.put("PromotionInfo->PromotionEndTime",YMTDateUtil.getBeforeOrNextDay(3));
			map.put("PromotionInfo->PromotionType","1");
			map.put("PromotionInfo->IsLimitProduct",true);
			String productId = "74b5c894-8ee7-47d2-a787-911d5602bb68";
			map.put("PromotionInfo->ProductIds",new JSONArray("[74b5c894-8ee7-47d2-a787-911d5602bb68]"));
			JSONObject retBea = new CreateSellerPromotionCall().callAndGetReturnData(map);
			
			int promotionId = retBea.getJSONObject("Data").getInt("PromotionId");
			Map<String, Object> mongoDataMap = new SellerPromotionInfoWapper().getPromotionId(promotionId);
			JSONObject GetProductPromotionPolicyBean = new GetPromotionProductByIdCall().callAndGetReturnData(MapUtil.hashMap("sellerId", sellerId,"ProductId", productId));
			
			JSONArray productIds = new JSONArray(mongoDataMap.get("ProductIds").toString());
			AssertService.checkArray(productIds,new JSONArray("[74b5c894-8ee7-47d2-a787-911d5602bb68]"));
			Logger.verifyEquals(true, mongoDataMap.get("IsLimitProduct"), "是否为排除商品活动");
			Logger.verifyEquals(200,GetProductPromotionPolicyBean.getInt("Code"), "响应报文-返回码");			
			Logger.verifyIsNull(GetProductPromotionPolicyBean.getString("Data"), "查询结果为空");
			
		} catch (Exception e) {
		
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductPromotionPolicy_004() {
		Logger.start(true, "商品在已结束的满减活动中，查询结果为空");
		try {
			int sellerId = 20323743;
			SellerPromotionCallService.checkIsExistPromotion(sellerId);
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("PromotionInfo->PromotionName","满2件减8.8-自动");
			map.put("PromotionInfo->PromotionBeginTime",YMTDateUtil.getBeforeOrNextDay(-1));
			map.put("PromotionInfo->PromotionEndTime",YMTDateUtil.getBeforeOrNextDay(3));
			map.put("PromotionInfo->PromotionType","1");
			map.put("PromotionInfo->IsLimitProduct",true);
			String productId = "74b5c894-8ee7-47d2-a787-911d5602bb68";
			map.put("PromotionInfo->ProductIds",new JSONArray("[74b5c894-8ee7-47d2-a787-911d5602bb68]"));
			JSONObject retBea = new CreateSellerPromotionCall().callAndGetReturnData(map);
			SellerPromotionCallService.checkIsExistPromotion(sellerId);
			int promotionId = retBea.getJSONObject("Data").getInt("PromotionId");
			Map<String, Object> mongoDataMap = new SellerPromotionInfoWapper().getPromotionId(promotionId);
			JSONObject GetProductPromotionPolicyBean = new GetPromotionProductByIdCall().callAndGetReturnData(MapUtil.hashMap("sellerId", sellerId,"ProductId", productId));
			
			JSONArray productIds = new JSONArray(mongoDataMap.get("ProductIds").toString());
			AssertService.checkArray(productIds,new JSONArray("[74b5c894-8ee7-47d2-a787-911d5602bb68]"));
			Logger.verifyEquals(true, mongoDataMap.get("IsLimitProduct"), "是否为排除商品活动");
			Logger.verifyEquals(200,GetProductPromotionPolicyBean.getInt("Code"), "响应报文-返回码");			
			Logger.verifyIsNull(GetProductPromotionPolicyBean.getString("Data"), "查询结果为空");
			
		} catch (Exception e) {
		
		}
	}
}
